package com.netty.server.handler;

import cn.hutool.core.util.HexUtil;
import com.netty.server.cmd.ByteResCmd;
import com.netty.server.sender.CmdResponse;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageCodec;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class ResCmdHandler extends MessageToMessageCodec<ByteResCmd, ByteResCmd> {

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, ByteResCmd byteResCmd, List<Object> list) throws Exception {
        byte[] data = byteResCmd.serializer();
        log.info("发送数据包: [{}]", HexUtil.encodeHexStr(data));
        list.add(Unpooled.wrappedBuffer(data));
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteResCmd byteResCmd, List<Object> list) {
        //向下转发
        list.add(new CmdResponse(byteResCmd.getCmdId(), byteResCmd));
    }
}
